package com.zhang.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.domain.Department;
import com.zhang.domain.DepartmentVO;
import com.zhang.mapper.DepartmentMapper;
import com.zhang.query.DepartmentQuery;
import com.zhang.service.IDepartmentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zh
 * @create 2022-02-21 14:00
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class DepartmentServiceImpl implements IDepartmentService {
    @Resource
    private DepartmentMapper mapper;
    @Transactional
    @Override
    public void add(DepartmentVO d) {
        mapper.save(d);
    }
    @Transactional
    @Override
    public void update(DepartmentVO d) {
        mapper.update(d);
    }
    @Transactional
    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public Department selectById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public List<Department> selectAll() {
        return mapper.loadAll();
    }

    @Override
    public PageInfo<Department> selectData(DepartmentQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Department> departments = mapper.queryData(query);
        Page<Department> page = (Page<Department>) departments;
        return page.toPageInfo();
    }

    @Override
    public Long queryEmpId(String managerName) {
        if(managerName==null){
            return null;
        }else {
            return mapper.queryEmpId(managerName);
        }

    }

    @Override
    public Long queryDeptId(String parentName) {
        if(parentName==null){
            return null;
        }else {
            return mapper.queryDeptId(parentName);
        }
    }

    @Override
    public void deleteAll(Long[] id) {
        mapper.dels(id);
    }

    @Override
    public List<String> selAllMan() {
        return mapper.selAllMan();
    }
}
